package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import t327.pojo.ProductionPlan;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.until.Result;
import t327.service.iter.IproductionplanService;

import java.util.List;

@Controller
@RequestMapping("productionplans")
public class productionplanController {
        @Autowired
        private IproductionplanService iproductionplanService;

    @RequestMapping("addproductionPlan")
    @ResponseBody
    public Result addproductionPlan(@RequestBody ProductionPlan productionPlan){
        int add =iproductionplanService.addproductionplan(productionPlan);
        if (add > 0) {
            return Result.success();
        } else {
            return new Result(202, "新增失败", false, null);
        }

    }

    @RequestMapping("deleteproductionPlanById")
    @ResponseBody
    public Result deleteproductionPlanById(Long productionPlanId) {
        System.out.println(productionPlanId + "前端参数");
        int i = iproductionplanService.deleteproductionplanById(productionPlanId);
        System.out.println(i + "修改结果");
        if (i > 0) {
            return Result.success();
        } else {
            return new Result(202, "删除失败", false, null);
        }

    }

    @RequestMapping("getProductionPlan")
    @ResponseBody
    private LuiTableEntity getProductionPlan(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit") Integer pageSize) {
        List<ProductionPlan> productionPlans1 = iproductionplanService.queryAll();
        LuiTableEntity luiTableEntity = new LuiTableEntity<ProductionPlan>();
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ProductionPlan> menuPageInfo = new PageInfo<>(productionPlans1);
        List<ProductionPlan> productionPlans = menuPageInfo.getList();
        long total = menuPageInfo.getTotal();
        if (productionPlans != null && productionPlans.size() > 0) {
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(productionPlans);
        } else {
            luiTableEntity.setCode("203");
            luiTableEntity.setMsg("查询失败");
        }
        return luiTableEntity;
    }

    @RequestMapping("updateProductionPlan")
    @ResponseBody
    public String updateProductionPlan(@RequestBody ProductionPlan productionPlan){
//        // 获取原始数据
//        ProductionPlan originalPlan = iproductionplanService.queryProductionPlan(productionPlan.getProductionPlanId());
//
//        // 将不可编辑字段的值赋给更新请求中的字段
//        productionPlan.setProductionPlanId(originalPlan.getProductionPlanId());

//        productionPlan.setOrderId(originalPlan.getOrderId());
//
//        // 执行更新操作
//        int updateResult = iproductionplanService.updateproductionplan(productionPlan);
//
//        if (updateResult > 0) {
//            return Result.success(); // 更新成功
//        } else {
//            return Result.failure(); // 更新失败
//        }


        return iproductionplanService.updateproductionplan(productionPlan)>0?"success":"fail";


    }
}
